﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Common;
using UnityEngine.SceneManagement;

public class LoginPanel : MonoBehaviour {

    public GameObject player;

    public Button Button_login;
    public Button Button_register;
    public InputField InputField_username;
    public InputField InputField_password;
    public Text Text_hintMessage;

    public GameObject registerPanel;

    private LoginRequest loginRequest;
    void Awake () {
        EventTriggerListener.Get(Button_login.gameObject, null).onClick = OnClick_Button_login;
        EventTriggerListener.Get(Button_register.gameObject, null).onClick = OnClick_Button_register;
    }
    void Start()
    {
        loginRequest = GetComponent<LoginRequest>();
    }

    void OnClick_Button_login(GameObject go, object data = null)
    {
        Debug.Log("OnClick_Button_login");

        Text_hintMessage.text = "";
        loginRequest.Username = InputField_username.text;
        loginRequest.Password = InputField_password.text;
        loginRequest.DefaultRequest();
    }

    void OnClick_Button_register(GameObject go, object data = null)
    {
        Debug.Log("OnClick_Button_register");

        this.gameObject.SetActive(false);
        registerPanel.gameObject.SetActive(true);
    }

    public void OnLoginResponse(ReturnCode returnCode)
    {
        if (returnCode == ReturnCode.Success)
        {
            //跳转下一场景
            Debug.Log("登录成功！");
            Text_hintMessage.text = "登录成功！";

            SceneManager.LoadScene("Game");
            //player.gameObject.SetActive(true);
        }
        else
        {
            Text_hintMessage.text = "用户名或密码错误！";
        }
    }
}
